﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using UIT.Style;

namespace UIT.Converters
{
    public class BrushConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            string s = value as string;
            if (s == null)
                return base.ConvertFrom(context, culture, value);
            return new SolidColorBrush(ColorConverter.FromString(s));
        }

        public static string ToString(Brush brush)
        {
            SolidColorBrush solidBrush = brush as SolidColorBrush;
            if (solidBrush != null)
                return ColorConverter.ToString(solidBrush.Color);
            GradientColorBrush gradientBrush = brush as GradientColorBrush;
            if (gradientBrush != null)
                return ColorConverter.ToString(gradientBrush.GradientStops.Last().Color);
            return null;
        }
    }
}
